package acwing._2LanQiao._9DP;
/**
 * @Project : AlgorithmLearning
 * @Package : ACWing._2LanQiao._9DP
 * @File : Q1047.java
 * @Author : WangRuoyu
 * @Date : 2023/3/23 15:16
 */

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Q1047 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] words = br.readLine().split(" ");

        int n = Integer.parseInt(words[0]);
        int k = Integer.parseInt(words[1]);

        int[] d = new int[n + 1];
        for (int i = 1; i <= n; ++i) {
            d[i] = Integer.parseInt(br.readLine());
        }

        int[][] f = new int[n + 1][k];
        for (int i = 1; i < k; ++i) {
            f[0][i] = Integer.MIN_VALUE; // f[0]这一行只有发f[0][0]有意义，无意义的值设为最小值，表示不存在
        }
        for (int i = 1; i <= n; ++i) {
            for (int j = 0; j < k; ++j) {
                f[i][j] = Math.max(f[i - 1][j], f[i - 1][(j - d[i] % k + k) % k] + d[i]);
            }
        }
        System.out.println(f[n][0]);
    }
}